package com.ithe.canteen.controller;

import com.ithe.canteen.common.Result;
import com.ithe.canteen.pojo.Customer;
import com.ithe.canteen.pojo.Manager;
import com.ithe.canteen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/customerLogin")
    public Result customerLogin(@RequestBody Customer customer) {
        String username = customer.getUsername();
        String password = customer.getPassword();
        Customer queryResult = userService.customerLogin(username, password);
        if (queryResult != null) {
            return Result.success(queryResult);
        }
        return Result.error("未查到此用户");
    }

    @PostMapping("/updateFlavor")
    public Result updateFlavor(@RequestBody Customer customer) {
        System.out.println(customer);
        String customerID = customer.getCustomerID();
        String loveFood = customer.getLoveFood();
        String hateFood = customer.getHateFood();
        boolean updateResult = userService.updateFlavor(customerID, loveFood, hateFood);
        if (updateResult) {
            return Result.success();
        }
        return Result.error("更新失败");
    }

    @PostMapping("/insertCustomer")
    public Result insertCustomer(@RequestBody Customer customer) {
        String nickName = customer.getNickname();
        String userName = customer.getUsername();
        String password = customer.getPassword();
        boolean insertResult = userService.insertCustomer(nickName, userName, password);
        if (insertResult)
            return Result.success();
        return Result.error("注册失败");
    }

    @PostMapping("/manager/login")
    public Result managerLogin(@RequestBody Manager manager) {
        String username = manager.getUsername();
        String password = manager.getPassword();
        Manager queryResult = userService.managerLogin(username, password);
        if (queryResult != null) {
            return Result.success(queryResult);
        }
        return Result.error("未查到此用户");
    }

}
